package com.ansion.commonchat.contact.bean;

import com.ansion.basedb.dao.model.UserModel;
import com.ansion.string.StringUtils;
import com.ansion.utils.LanguageSettingHelper;

import java.util.Comparator;


/**
 *
 */
public class ContactsItemSort implements Comparator<ContactBaseBean> {

    @Override
    public int compare(ContactBaseBean lhs, ContactBaseBean rhs) {
        if (lhs.getUserModel().isBabaForSort() != rhs.getUserModel()
                .isBabaForSort()) {
            return rhs.getUserModel().isBabaForSort()
                    - lhs.getUserModel().isBabaForSort();
        }
        int compare = compareWithArab(lhs.getUserModel(),
                rhs.getUserModel());
        if (compare == 0) {
            return StringUtils.compareString(lhs.getSortKey(),
                    rhs.getSortKey());
        }
        return compare;
    }

    public static int compareWithArab(UserModel a, UserModel b) {
        if (a.isSymbol() == b.isSymbol()) {
            return 0;
        }
        if (a.isSymbol() && !b.isSymbol()) {
            return -1;
        } else if (!a.isSymbol() && b.isSymbol()) {
            return 1;
        } else {
            if (a.getIsArabic()) {
                if (b.getIsArabic()) {
                    return 0;
                } else {
                    if (LanguageSettingHelper.isArLanguage()) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            } else {
                if (!b.getIsArabic()) {
                    return 0;
                } else {
                    if (LanguageSettingHelper.isArLanguage()) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            }

        }

    }

}
